import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def contiguity_item(ID):
    i = 0
    ROW = spContiguity.shape[0]
    #print ROW
    contiguityItem = []
    j = 0
    
    while i < ROW:
        if spContiguity[i,0]== ID:
            contiguityItem.append(spContiguity[i,1])
            j = j + 1
        i = i + 1
    if j == 1:
        print ID, "does not have contiguity items."
    #elif j == 
    return contiguityItem

def global_EBS():
    sumID, sumOb, sumPop = attri.sum(axis=0)
    #print "sum of cancer is ", sumOb, ", sum of population is ",  sumPop
    aveID, aveOb, avePop = attri.mean(axis=0)
    #print "mean of cancer is ", aveOb, ", mean of population is ",  avePop

    
    u = sumOb/sumPop

    iLen = attri.shape
    #print iLen
    i = 0
    
    sigma = 0
    while i < iLen[0]:
        sigma = attri[i][2]*(attri[i][1]/attri[i][2]-u)*(attri[i][1]/attri[i][2]-u) + sigma        
        i = i + 1
    sigma = sigma/sumPop-u/avePop
    #sigma = temp

    
    pi_EB = np.array([])
    
    i = 0    
    while i < iLen[0]:
        if sigma > 0:
            temp = 0
            w = sigma/(sigma + u/attri[i][2])
            temp = w*attri[i][1]/attri[i][2]+(1-w)*u
            temp = temp * 100
        else:
            temp = u*100
        pi_EB = np.append(pi_EB, temp)
        i = i + 1
    #print pi_EB
    print "============================Global EB===================================="
    print "mu = ", u, "sigma = ", sigma
    '''
    alpha = u*u/sigma
    beta = u/sigma
    pi = np.array([])
    i = 0
    while i < iLen[0]:
        temp = 0
        temp = (attri[i][1]+alpha)/(attri[i][2]+beta)
        temp = temp * 100
        pi = np.append(pi, temp)
        i = i + 1
    #print pi

    i = 0
    while i < iLen[0]:
        print pi_EB[i], pi[i]
        i = i + 1
    '''
    return pi_EB
    
def local_EBS(attri):
    #attri: [id, cancer, pop]
    pi_EB = np.array([])
    iLen = attri.shape
    #print iLen
    i = 0
    sigma = np.array([])
    mu = np.array([])
    cong_sumpop = np.array([])
    #cong_avepop = np.array([])
    while i < iLen[0]:
        contiguityItem = contiguity_item(i)
        #mu_temp = 0
        temp_ob = 0
        temp_pop = 0
        j = 0
        temp_ID = 0
        #print len(contiguityItem)
        while j < len(contiguityItem):
            #print "j = ",j
            temp_ID = contiguityItem[j]
            temp_ob = temp_ob + attri[temp_ID][1]
            temp_pop = temp_pop + attri[temp_ID][2]
            #temp = attri[j][2]*(attri[j][1]/attri[j][2]-u)*(attri[j][1]/attri[j][2]-u) + temp
            j = j + 1
        mu = np.append(mu, temp_ob/temp_pop)
        cong_sumpop = np.append(cong_sumpop, temp_pop)
        #cong_avepop = np.append(cong_avepop, temp_pop/len(contiguityItem))
        i = i + 1

    i = 0
    while i < iLen[0]:
        contiguityItem = contiguity_item(i)
        j = 0
        temp_sigma = 0
        while j < len(contiguityItem):
            temp_ID = contiguityItem[j]
            temp_sigma = attri[temp_ID][2]*(attri[temp_ID][1]/attri[temp_ID][2]-mu[temp_ID])*(attri[temp_ID][1]/attri[temp_ID][2]-mu[temp_ID]) + temp_sigma
            j = j + 1
        temp_sigma = temp_sigma/cong_sumpop[i] - mu[i]*len(contiguityItem)/cong_sumpop[i]
        sigma = np.append(sigma, temp_sigma)
        i = i + 1
    #print sigma[1]
    
    i = 0
    while i < iLen[0]:
        if sigma[i]<0:
            temp = mu[i]
        else:
            temp_weight = 0
            temp_weight = sigma[i]/(sigma[i]+mu[i]/attri[i][2])
            temp = 0
            temp = temp_weight*attri[i][1]/attri[i][2]+(1-temp_weight)*mu[i]
            temp = temp
        pi_EB = np.append(pi_EB, temp)
        i = i + 1
    #print pi_EB
    return pi_EB
  
    
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    unitCSV = 'C:/_DATA/CancerData/SatScan/mult6000/three16_format.csv'
    contiguityCSV = 'C:/_DATA/CancerData/SatScan/NortheeaternUS_re_contiguity.csv'
    dataMatrix = build_data_list(unitCSV)
    spContiguity = build_data_list(contiguityCSV)
    #print len(dataMatrix)
    dataused = np.zeros((len(dataMatrix),3))
    #dataused[:,1] = dataMatrix[:, 2]
    dataused[:,2] = dataMatrix[:,1]
    
    
    lebs_rate = []
    for i in range(0,1000):
        print i
        dataused[:,1] = dataMatrix[:, 2+i]
        temp_rate = local_EBS(dataused)
        lebs_rate.append(temp_rate)
    lebs_rate = np.array(lebs_rate)
    lebs_rate.shape = (-1, len(dataused))
    lebs_rate = np.transpose(lebs_rate)
    print lebs_rate
    filePath = 'C:/_DATA/CancerData/SatScan/mult6000/three16_lebs_rate_newCTG.csv'
    np.savetxt(filePath, lebs_rate, delimiter=',')
    
    print "end at " + getCurTime()
    print "==========================="

           
